package com.mo.demo.ehcache;

import lombok.extern.slf4j.Slf4j;
import org.ehcache.Cache;
import org.ehcache.CacheManager;
import org.ehcache.config.builders.CacheManagerBuilder;
import org.ehcache.xml.XmlConfiguration;

@Slf4j
public class EhcacheExpireDemo {
    public static void main(String[] args) throws InterruptedException {

        XmlConfiguration xmlConfiguration = new XmlConfiguration(EhcacheExpireDemo.class.getResource("/ehcache.xml"));
        CacheManager manager = CacheManagerBuilder.newCacheManager(xmlConfiguration);
        manager.init();

        Cache<Number,String> ttlCache = manager.getCache("ttlCache",Number.class,String.class);
        Cache<Number,String> ttiCache = manager.getCache("ttiCache",Number.class,String.class);

        int count = 3;

        log.info("=== TTL ===");
        for (int i = 0; i < count; i++) {
          ttlCache.put(i,i+""+i);
          log.info("key={}, value={}",i,ttlCache.get(i));
        }
        new Thread(()->{
            try {
                readCache(ttlCache,"ttlCache");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        Thread.sleep(3000);

        log.info("3秒后：");
        for (int i = 0; i < count; i++) {
            log.info("key={}, value={}",i,ttlCache.get(i));
        }

        log.info("=== TTI ===");
        for (int i = 0; i < count; i++) {
            ttiCache.put(i,i+""+i);
            log.info("key={}, value={}",i,ttiCache.get(i));
        }

        new Thread(()->{
            try {
                readCache(ttiCache,"ttiCache");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();

        Thread.sleep(3000);

        log.info("3秒后的缓存状态：");
        for (int i = 0; i < count; i++) {
            log.info("key={}, value={}",i,ttiCache.get(i));
        }

        // 输出：
        // 05:55:17.151 [main] INFO com.mo.demo.ehcache.EhcacheExpireDemo - === TTL ===
        // 05:55:17.159 [main] INFO com.mo.demo.ehcache.EhcacheExpireDemo - key=0, value=00
        // 05:55:17.159 [main] INFO com.mo.demo.ehcache.EhcacheExpireDemo - key=1, value=11
        // 05:55:17.159 [main] INFO com.mo.demo.ehcache.EhcacheExpireDemo - key=2, value=22
        // 05:55:19.170 [Thread-2] INFO com.mo.demo.ehcache.EhcacheExpireDemo - [ttlCache] 间隔2秒 访问 i=2 的缓存:22
        // 05:55:20.174 [main] INFO com.mo.demo.ehcache.EhcacheExpireDemo - 3秒后：
        // 05:55:20.175 [main] INFO com.mo.demo.ehcache.EhcacheExpireDemo - key=0, value=null
        // 05:55:20.175 [main] INFO com.mo.demo.ehcache.EhcacheExpireDemo - key=1, value=null
        // 05:55:20.175 [main] INFO com.mo.demo.ehcache.EhcacheExpireDemo - key=2, value=null
        // 05:55:20.175 [main] INFO com.mo.demo.ehcache.EhcacheExpireDemo - === TTI ===
        // 05:55:20.176 [main] INFO com.mo.demo.ehcache.EhcacheExpireDemo - key=0, value=00
        // 05:55:20.176 [main] INFO com.mo.demo.ehcache.EhcacheExpireDemo - key=1, value=11
        // 05:55:20.176 [main] INFO com.mo.demo.ehcache.EhcacheExpireDemo - key=2, value=22
        // 05:55:21.170 [Thread-2] INFO com.mo.demo.ehcache.EhcacheExpireDemo - [ttlCache] 间隔2秒 访问 i=2 的缓存:null
        // 05:55:22.180 [Thread-3] INFO com.mo.demo.ehcache.EhcacheExpireDemo - [ttiCache] 间隔2秒 访问 i=2 的缓存:22
        // 05:55:23.185 [main] INFO com.mo.demo.ehcache.EhcacheExpireDemo - 3秒后的缓存状态：
        // 05:55:23.185 [main] INFO com.mo.demo.ehcache.EhcacheExpireDemo - key=0, value=null
        // 05:55:23.186 [main] INFO com.mo.demo.ehcache.EhcacheExpireDemo - key=1, value=null
        // 05:55:23.186 [main] INFO com.mo.demo.ehcache.EhcacheExpireDemo - key=2, value=22
        // 05:55:23.185 [Thread-2] INFO com.mo.demo.ehcache.EhcacheExpireDemo - [ttlCache] 间隔2秒 访问 i=2 的缓存:null
        // 05:55:24.186 [Thread-3] INFO com.mo.demo.ehcache.EhcacheExpireDemo - [ttiCache] 间隔2秒 访问 i=2 的缓存:22
        // 05:55:25.189 [Thread-2] INFO com.mo.demo.ehcache.EhcacheExpireDemo - [ttlCache] 间隔2秒 访问 i=2 的缓存:null
        // 05:55:26.197 [Thread-3] INFO com.mo.demo.ehcache.EhcacheExpireDemo - [ttiCache] 间隔2秒 访问 i=2 的缓存:22
        // 05:55:27.197 [Thread-2] INFO com.mo.demo.ehcache.EhcacheExpireDemo - [ttlCache] 间隔2秒 访问 i=2 的缓存:null
        // 05:55:28.199 [Thread-3] INFO com.mo.demo.ehcache.EhcacheExpireDemo - [ttiCache] 间隔2秒 访问 i=2 的缓存:22
        // 05:55:29.206 [Thread-2] INFO com.mo.demo.ehcache.EhcacheExpireDemo - [ttlCache] 间隔2秒 访问 i=2 的缓存:null
        // 05:55:30.199 [Thread-3] INFO com.mo.demo.ehcache.EhcacheExpireDemo - [ttiCache] 间隔2秒 访问 i=2 的缓存:22
        // 05:55:31.213 [Thread-2] INFO com.mo.demo.ehcache.EhcacheExpireDemo - [ttlCache] 间隔2秒 访问 i=2 的缓存:null
        // 05:55:32.211 [Thread-3] INFO com.mo.demo.ehcache.EhcacheExpireDemo - [ttiCache] 间隔2秒 访问 i=2 的缓存:22
        // 05:55:33.217 [Thread-2] INFO com.mo.demo.ehcache.EhcacheExpireDemo - [ttlCache] 间隔2秒 访问 i=2 的缓存:null
        // 05:55:34.213 [Thread-3] INFO com.mo.demo.ehcache.EhcacheExpireDemo - [ttiCache] 间隔2秒 访问 i=2 的缓存:22

    }

    private static void readCache(Cache<Number, String> cache,String cacheName) throws InterruptedException {
        Thread.sleep(2000);
        log.info("[{}] 间隔2秒 访问 i=2 的缓存:{}",cacheName,cache.get(2));
        readCache(cache,cacheName);
    }
}
